package com.wwm.petadopt;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.util.Attributes;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class MoreActivity extends Activity {
    private String TAG = this.getClass().getSimpleName();

    private int mPosition;

    private RequestQueue mRequestQueue;


    private ProgressDialog pd;
    private ListView mListView;
    private VerticalListViewAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        final String petType=intent.getStringExtra("petType");
        ActionBar ab = getActionBar();
        if (ab != null)
        {
            View actionbarview=getLayoutInflater().inflate(R.layout.actionbar,null);
            ((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.more);

            ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            ab.setCustomView(actionbarview);

        }
        setContentView(R.layout.activity_more);

        mListView = (ListView) findViewById(R.id.vertical_listview);
        //Button bt = (Button) view.findViewById(R.id.btn_news);
        // bt.setOnClickListener(new View.OnClickListener() {

        // @Override
        // public void onClick(View v) {

        mAdapter = new VerticalListViewAdapter(this);

        mRequestQueue = Volley.newRequestQueue(this);
        Map<String, String> map = new HashMap<String, String>();
        map.put("RequestNum", "20");
        map.put("RequestNumType", petType);

        JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.GET_PET_INFO_WITH_NUM_URL, new JSONObject(map), new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                Log.i("petadopt", response.toString());
                //parseJSON(response);
                //petIDArray = new ArrayList<Integer>();
                ArrayList<PetDetail> petDetailArray = new ArrayList<PetDetail>();


                try {
                    Iterator it = response.keys();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        String value = response.getString(key);
                        JSONObject j = response.getJSONObject(key);
                        PetDetail p = new PetDetail(j);
                        petDetailArray.add(p);
                        //petIDArray.add(Integer.parseInt(value));
                    }


                    mAdapter.setData(petDetailArray);
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.i("petadopt", error.getMessage());
            }
        });
        mRequestQueue.add(jr);



        mListView.setAdapter(mAdapter);
        mAdapter.setMode(Attributes.Mode.Single);


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ((SwipeLayout) (mListView.getChildAt(position - mListView.getFirstVisiblePosition()))).open(true);

            }
        });


        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("ListView", "OnTouch");
                return false;
            }
        });


        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("ListView", "onScrollStateChanged");
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });


        mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.e("ListView", "onItemSelected:" + position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.e("ListView", "onNothingSelected:");
            }
        });
        //  }
        //});


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_more, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
